//
//  AWRadioHomeController.m
//  EPA
//
//  Created by 请叫我灬阿文 on 16/4/22.
//  Copyright © 2016年 请叫我阿文. All rights reserved.
//

#import "AWRadioHomeController.h"
#import "LDTNetWorkRequestManager.h"
#import "AWRadioModel.h"
#import "AWRadioHomeCell.h"
#import "LDTCycleView.h"
#import "AWRadioCycleModel.h"
#import "LDTRadioDetailsController.h"
@interface AWRadioHomeController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)NSMutableArray *array;
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)LDTCycleView *cleView;
@property (nonatomic,strong)NSMutableArray *cycleArray;
@end

@implementation AWRadioHomeController
-(NSMutableArray *)array{
    if (!_array) {
        _array=[NSMutableArray new];
    }
    return _array;
}

-(NSMutableArray *)cycleArray{
    if (!_cycleArray) {
        _cycleArray=[NSMutableArray new];
    }
    return _cycleArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setViews];
    [self setDelegate];
    [self setData];
    // Do any additional setup after loading the view.
}
-(void)setViews{
   
    self.title = @"电台列表";
    self.tableView=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    //创建背景View
    UIImageView *backView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    backView.image = [UIImage imageNamed:@"毛玻璃"];
    self.tableView.backgroundView = backView;
    //毛玻璃
    UIBlurEffect *abEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc]initWithEffect:abEffect];
    effectView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    [backView addSubview:effectView];

    
    
    
    
    [self.view addSubview:self.tableView];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"AWRadioHomeCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    
    [self.tableView dequeueReusableHeaderFooterViewWithIdentifier:@"headerView"];
    self.cleView=[[LDTCycleView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 230) InterVal:3];
    
    [self.tableView addSubview:self.cleView];
    
}
-(void)setDelegate{
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
}
-(void)setData{
    
    [LDTNetWorkRequestManager requestURL:[NSURL URLWithString:RADIOLIST_URL] Method:LDT_POST Params:@{@"client":@"1",@"deviceid":@"63A94D37-33F9-40FF-9EBB-481182338873",@"auth":@"",@"version":@"3.0.2"} Result:^(NSData *data, NSURLResponse *response, NSError *error) {
        NSDictionary *Radiodict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSArray *array=Radiodict[@"data"][@"alllist"];
        
        NSArray *arr=Radiodict[@"data"][@"carousel"];
        for (NSDictionary *itemDic in array) {
            
            AWRadioModel *model =[[AWRadioModel alloc]init];
            
            [model setValuesForKeysWithDictionary:itemDic];
            
            
            
            [self.array addObject:model];
            
            
            
            
        }
        
        for (NSDictionary *cycleDict in arr) {
            AWRadioCycleModel *model=[AWRadioCycleModel new];
            [model setValuesForKeysWithDictionary:cycleDict];
            NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:model.img]];
            UIImage *img=[UIImage imageWithData:data];
            [self.cycleArray addObject:img];
        }
        
        [[NSOperationQueue mainQueue]addOperationWithBlock:^{
            [self.tableView reloadData];
            self.cleView.imgNameArray=self.cycleArray;
        }];
     
        
    }];

}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AWRadioHomeCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.model=self.array[indexPath.row];
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *headerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 200)];
    
    tableView.tableHeaderView=headerView;
    return headerView;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 30;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    AWRadioModel *model = [self.array objectAtIndex:indexPath.row];
    
    
    LDTRadioDetailsController *radioDetailsVC = [[LDTRadioDetailsController alloc]init];
    radioDetailsVC.radioModel = model;
    
    [self.navigationController pushViewController:radioDetailsVC animated:YES];
}














- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
